<?php

/**
 * main actions.
 *
 * @package    vvberp
 * @subpackage main
 * @author     Flo
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class mainActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->form = new CerereForm();
    if ($request->isMethod("POST"))
    {
      $this->form->bind($request->getParameter("cerere"));
      if ($this->form->isValid())
      {
        return $this->renderText("<pre>".print_r($this->form->getValues(), true)."</pre>");
      }
    }
  }

  public function executeCerere(sfWebRequest $r)
  {
    if ($r->isXmlHttpRequest())
    {
      if ($r->getParameter('hash') == 'anuleaza') $this->getUser()->getAttributeHolder()->remove('cerere');
      //print_r($r->getParameterHolder());
      return sfView::NONE;
    }
    
    if ($r->isMethod("GET"))
    {
      if ($r->getParameter('anuleaza')) $this->getUser()->getAttributeHolder()->remove('cerere');
    }
    
    if ($r->isMethod("POST"))
    {
      if ($idCerere = $r->getParameter("cerere[id]")) //edit
      {
        
      }
      $this->getUser()->setAttribute('cerere', $r->getParameter("cerere"));
      $this->redirect('main/cerere');
    }
    
    $this->tipuriFurnizori = TipFurnizorPeer::doSelect(new Criteria());
    
    $this->randuri = array();
    
    if ($this->cerere = CererePeer::retrieveByPK($id = $r->getParameter("id")))
    {
      //produse deja atasate cererii:
      //  in cazul edit
      foreach ($this->cerereProduse = $this->cerere->getCerereProdussJoinProdus() as $i => $cp)
      {
        $this->randuri[] = $this->getPartial('main/produs', array(
          'tipuriFurnizor' => $this->tipuriFurnizori,
          'obiect' => $cp, //pt produs deja existent si atasat cererii -> trimit la partial obiect de tip CPjoinP
          'cerere' => $this->cerere
        ));
      }
    }
    else
    {
      if ($id) throw new Exception('Idul cererii nu exista.');
      $this->cerere = new Cerere();
    }
    //  din sesiune
    if ($this->getUser()->hasAttribute("cerere")) 
    {
      $produseSesiune = $this->getUser()->getAttribute("cerere[produse][noi]");
      foreach ($produseSesiune as $i=>$pcs)
      {
      /*[1] => Array
            (
                [nume] => 
                [standard] => 
                [cod] => 
                [material] => 
                [dimensiune] => 
                [cantitate] => 
            )*/
        $this->randuri[] = $this->getPartial(('main/produs'), array('id'=>$i, 'tipuriFurnizori' => $this->tipuriFurnizori));
      }
    }
    if (count($this->randuri) < 1) $this->randuri[] = $this->getPartial(('main/produs'), array('id'=>1, 'tipuriFurnizori' => $this->tipuriFurnizori));
    $this->clienti = ClientPeer::doSelect(new Criteria());
  
  }
  
  public function executeAutocompleteProdus(sfWebRequest $r)
  {
    //if ($r->isXmlHttpRequest())
    $this->search = $r->getParameter("q");
    $this->limit = $r->getParameter("limit");
  }

}